package com.iman.manjusri.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.util.IdCreater;
import com.iman.common.util.PriceUtils;
import com.iman.manjusri.mappers.DrmBuyCartMapper;
import com.iman.manjusri.mappers.DrmBuyCourseMapper;
import com.iman.manjusri.mappers.DrmBuyOrderDetailMapper;
import com.iman.manjusri.mappers.DrmBuyOrderMapper;
import com.iman.manjusri.mappers.DrmPayTraceMapper;
import com.iman.manjusri.mappers.models.DrmBuyCart;
import com.iman.manjusri.mappers.models.DrmBuyCourse;
import com.iman.manjusri.mappers.models.DrmBuyOrder;
import com.iman.manjusri.mappers.models.DrmBuyOrderDetail;
import com.iman.manjusri.mappers.models.DrmPayTrace;
import com.iman.manjusri.service.BuyOrderService;
import com.iman.manjusri.vo.CalculateAmountVo;
import com.iman.manjusri.vo.DrmCourseVo;

/*
 * 
 * *************************************************************************
 * @文件名称: BuyCartServiceImpl.java
 *
 * @包路径  : com.iman.manjusri.service.impl 
 *				 
 * @版权所有:   TSW   科技有限公司 (C) 2014
 *
 * @类描述:  
 * 
 * @创建人:   wangxiaolei  
 *
 * @创建时间: 2015年3月15日 - 下午7:29:10 
 *
 * @修改记录:
 -----------------------------------------------------------------------------------------------
 时间						|		修改人		|		修改的方法		|		修改描述                                                                
 -----------------------------------------------------------------------------------------------
 |					|					|                                       
 ----------------------------------------------------------------------------------------------- 	

 **************************************************************************
 */
@Service
public class BuyOrderServiceImpl implements BuyOrderService {

	@Inject
	DrmBuyOrderMapper drmBuyOrderMapper;
	
	@Inject
	DrmPayTraceMapper drmPayTraceMapper;
	
	@Inject
	DrmBuyOrderDetailMapper drmBuyOrderDetailMapper;
	@Inject
	DrmBuyCourseMapper drmBuyCourseMapper;
	@Inject
	DrmBuyCartMapper drmBuyCartMapper;
	@Inject
	TemplateQLService templateQLService;

	@Override
	public DrmBuyOrder getBuyOrder(String orderId) {
		return drmBuyOrderMapper.selectByPrimaryKey(orderId);
	}

	@Override
	public DrmBuyOrder addBuyOrder(Long userId, CalculateAmountVo calculate, List<DrmCourseVo> lstCart) {
		Date date = new Date();
		// 生成订单主表
		DrmBuyOrder order = new DrmBuyOrder();
		if (calculate.getPayAmount().compareTo(BigDecimal.ZERO) == 0) {// 学习币直接支付成功
			order.setOrderSt(4);
			order.setPayTp(4);
			order.setPayComfirm(date);
		} else {// 待其他方式支付
			order.setOrderSt(1);
		}
		String orderId = IdCreater.getId().toString();
		order.setOrderId(orderId);
		order.setCreateDt(date);
		order.setUserId(userId);
		order.setCommitDt(date);
		order.setConfirmDt(date);
		order.setPayPrice(PriceUtils.YuanToCoinInt(calculate.getPayAmount().toString()));
		order.setPayCorn(PriceUtils.YuanToCoinInt(calculate.getUseCredit().toString()));
		order.setPayDiscount(PriceUtils.YuanToCoinInt(calculate.getPromotion().toString()));
		drmBuyOrderMapper.insertSelective(order);

		
		//点单支付轨迹
		DrmPayTrace payTrace = new DrmPayTrace();
		payTrace.setComfrinYn(0);//未确认
		payTrace.setConfrimDesc(null);
		payTrace.setConfrimUid(userId);
		payTrace.setId( IdCreater.getId());
		payTrace.setOrderId( orderId);
		payTrace.setPayDt(new Date());
		payTrace.setPayPrice((long)PriceUtils.YuanToCoinInt(calculate.getPayAmount().toString()) );
		payTrace.setPayState( 0);//生成订单
		payTrace.setPayTp( null);//后补上
		payTrace.setPayUserid(userId);
		
		drmPayTraceMapper.insertSelective(payTrace );
		
		
		
		for (DrmCourseVo course : lstCart) {
			// 新增订单明细表记录
			DrmBuyOrderDetail orderDtl = new DrmBuyOrderDetail();
			orderDtl.setOrderId(order.getOrderId());
			orderDtl.setCourseId(course.getCourseId());
			orderDtl.setCreateDt(date);
			orderDtl.setCartId(course.getCartId());
			drmBuyOrderDetailMapper.insertSelective(orderDtl);

			// 新增购买课程表记录
			DrmBuyCourse buyCourse = new DrmBuyCourse();
			buyCourse.setCourseId(course.getCourseId());
			buyCourse.setUserId(userId);
			buyCourse.setBuyDt(date);
			// buyCourse.setOrderPrice(new Double(order.getPayPrice() * 100).longValue());
			buyCourse.setOrgPrice(course.getOrgPrice());
			buyCourse.setCurrentPrice(course.getCurPrice());
			buyCourse.setOrderId(order.getOrderId());
			buyCourse.setComfirmYn(order.getOrderSt() == 4 ? 1 : 0);// 直接确认或待确认
			drmBuyCourseMapper.insertSelective(buyCourse);

			// 更新购物车状态
			DrmBuyCart cart = new DrmBuyCart();
			cart.setCartId(course.getCartId());
			cart.setCourseId(course.getCourseId());
			//加入购物车 等待付款 
			cart.setCartSt(1);
			//cart.setCartSt(3);这个状态是 点击了付款进入付款页面，在支付平台里面修改
			if (order.getOrderSt() == 4) {
				cart.setPayDt(date);
			}
			drmBuyCartMapper.updateByPrimaryKeySelective(cart);
		}

		// 更新剩余学习币
		Map<String, Object> mapInfo = new HashMap<String, Object>();
		mapInfo.put("userId", userId);
		mapInfo.put("useCredit", order.getPayCorn());
		templateQLService.executeSQLUpdate("update_user_learncredit", mapInfo);

		return order;
	}

}
